# coding:utf-8
import os
import sys

from PySide6.QtCore import QUrl, QSize, QEventLoop, QTimer
from PySide6.QtGui import QIcon, QDesktopServices
from PySide6.QtWidgets import QApplication
from qfluentwidgets import (
    NavigationItemPosition,
    MessageBox,
    NavigationAvatarWidget,
    SplitFluentWindow,
    SplashScreen,
)
from qfluentwidgets import FluentIcon as FIF

from .students.student_interface import StudentInterface
from .video.video_interface import VideoInterface
from .camera.camera_interface import CameraInterface
from .web_view.web_view_interface import WebViewInterface
from .markdown.markdown_interface import MarkdownInterface


def resource_path(relative_path):
    """获取资源的绝对路径"""
    if hasattr(sys, "_MEIPASS"):
        return os.path.join(sys._MEIPASS, relative_path)  # type: ignore
    return os.path.join(os.path.abspath("."), relative_path)


class Window(SplitFluentWindow):
    def __init__(self):
        super().__init__()

        self.studemt_interface = StudentInterface()
        self.video_interface = VideoInterface()
        self.camera_interface = CameraInterface()
        self.web_view_interface = WebViewInterface(self)
        self.markdown_interface = MarkdownInterface()
        self.initNavigation()
        self.resize(900, 700)

        # 使用资源路径函数
        self.setWindowIcon(QIcon(resource_path("./resources/images/logo.png")))

        desktop = QApplication.screens()[0].availableGeometry()
        w, h = desktop.width(), desktop.height()
        self.move(w // 2 - self.width() // 2, h // 2 - self.height() // 2)

        self.splashScreen = SplashScreen(self.windowIcon(), self)
        self.splashScreen.setIconSize(QSize(102, 102))
        self.show()
        self.createSubInterface()
        self.splashScreen.finish()

    def createSubInterface(self):
        loop = QEventLoop(self)
        QTimer.singleShot(2000, loop.quit)
        loop.exec()

    def initNavigation(self):
        self.addSubInterface(self.studemt_interface, FIF.RINGER, "Students")
        self.addSubInterface(self.video_interface, FIF.VIDEO, "Video")
        self.addSubInterface(self.camera_interface, FIF.CAMERA, "Camera")
        self.addSubInterface(self.web_view_interface, FIF.FIT_PAGE, "Web View")
        self.addSubInterface(self.markdown_interface, FIF.DOCUMENT, "Markdown Editor")

        # 使用资源路径函数
        self.navigationInterface.addWidget(
            routeKey="Info",
            widget=NavigationAvatarWidget(
                "Info", resource_path("./resource/images/logo.png")
            ),
            onClick=self.showMessageBox,
            position=NavigationItemPosition.BOTTOM,
        )

        self.navigationInterface.addItem(
            routeKey="settingInterface",
            icon=FIF.SETTING,
            text="设置",
            position=NavigationItemPosition.BOTTOM,
        )

        self.navigationInterface.setExpandWidth(280)

    def showMessageBox(self):
        w = MessageBox(
            "支持作者🥰",
            "个人开发不易，如果这个项目帮助到了您，可以考虑请作者喝一瓶快乐水🥤。您的支持就是作者开发和维护项目的动力🚀",
            self,
        )
        w.yesButton.setText("来啦老弟")
        w.cancelButton.setText("下次一定")

        if w.exec():
            QDesktopServices.openUrl(QUrl("https://afdian.net/a/zhiyiYo"))
